package jdbc;

import java.sql.*;

public class JDBCDemo5 {
    public static void main(String[] args) throws SQLException, ClassNotFoundException {
        // 1.注册驱动Class.forName()
        Class.forName("com.mysql.cj.jdbc.Driver");
        // 2.建立连接DriverManager.getConnection()
        String url = "jdbc:mysql://localhost:3307/tedu?characterEncoding=utf8&useSSL=false&serverTimezone=Asia/Shanghai&rewriteBatchedStatements=true";
        Connection connection = DriverManager.getConnection(url, "root", "root");
        // 3.创建Statement对象
        Statement statement = connection.createStatement();
        String sql = "SELECT id,name,age,salary,title " +
                "FROM teacher";
        ResultSet resultSet = statement.executeQuery(sql);
        while (resultSet.next()) {
            int id = resultSet.getInt("id");
            String name = resultSet.getString("name");
            String title = resultSet.getString("title");
            int age = resultSet.getInt("age");
            int salary = resultSet.getInt("salary");
            System.out.println(id + "," + name + "," + title + "," + age + "," + salary);
        }
        connection.close();
    }
}
